package org.epam.szhuk.greenway.util;

import java.io.File;
import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.MediaRecorder.VideoEncoder;
import android.os.Environment;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.util.Log;

/**
 * Preferences for application
 * @author Sergey Zhuk
 *
 */
public class Preferences implements Cloneable{
	private static String TAG="GreenWayPreferences";
	//WARN: all const values have to be manually synchronized with their xml values
	public static final int SPEED_MODE_KMH=1;
	public static final int SPEED_MODE_MPH=2;
	
	public static final int REC_MAX_SIZE_NOMORE_FREE_SPACE=-10; 
	public static final int REC_MAX_SIZE_FOLDER_MAXSIZE_REACHED=-100;
	
	public static final String DATE_SAVE_MEDIAFILE_FORMAT="yyyyMMdd_HHmmss"; //+SSS
	public static final String DATE_TITLE_PLAYVIDEO_FORMAT="dd-MM-yyyy HH:mm";//"EEE, d MMM yyyy HH:mm:ss";
	public static final String DATE_WRITETOFILE_FORMAT = "HH:mm:ss.SSS yyyy-MM-dd";
	public static final String DATE_TITLE_FOR_LIST_FORMAT = "dd-MM-yyyy HH:mm:ss"; 
	public static final String DATE_TITLE_SPEED_CHART = "dd-MM-yyyy HH:mm:ss";
	public static final String DATE_TITLE_FOR_SELECT_SPEEDFILE_FORMAT="dd-MM-yyyy HH:mm:ss";
	
	public static final String FOLDER_THUMBS="thumbs";
	//public static final String FOLDER_TRACKS="tracks";
	public static final String SAVEFILE_EXTENSION=".txt";
	
	
	public static final int LOW_BAT_LEVEL=36;//in % level
	
	private static final float COEF_KMH = 3.6f;
	private static final float COEF_MPH = 2.2369f;
	
	//---general settings:
	private int speedMode;	

	//---GPS settings	
	private boolean useGps;
	//private boolean saveGpsDataToFile;
    
	
	
	// ---Location
	/**
	 * Follows current location position - makes it center on the map
	 */
	private boolean followNavigation;
	
	private boolean makeAlarmOnExcessPolution;
	
	public boolean isMakeAlarmOnExcessPolution() {
		return makeAlarmOnExcessPolution;
	}


	public void setMakeAlarmOnExcessPolution(boolean makeAlarmOnExcessPolution) {
		this.makeAlarmOnExcessPolution = makeAlarmOnExcessPolution;
	}

	//---Save files
	private String appFilesFolder;


	/**
	 * Constructor to load values from SharedPreferences
	 */
	public Preferences(Context context){
		 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
		 Map<String, ?> prefer=pref.getAll();
		 Log.w(TAG, "Preferences: "+ prefer.toString());
		 
		 speedMode = Integer.parseInt(pref.getString("listSpeed", String.valueOf(SPEED_MODE_KMH)));//To cover Android bug
		 useGps=pref.getBoolean("checkboxUseGPS", true);
		 //saveGpsDataToFile=pref.getBoolean("checkboxSaveGPS", true);
		 appFilesFolder=pref.getString("appFilesFolder", "greenway");
		 followNavigation=pref.getBoolean("checkboxFollowNavigation", true);
		 makeAlarmOnExcessPolution=pref.getBoolean("checkboxExcessPollution", false);
	}
	
	public Preferences clone(){	
		 try {
            Preferences result = (Preferences) super.clone();
    		result.setSpeedMode(speedMode);
    		result.setAppFilesFolder(new String(appFilesFolder));
    		result.setSpeedMode(speedMode);
    		result.setUseGps(useGps);
    		result.setFollowNavigation(followNavigation);
    		result.setMakeAlarmOnExcessPolution(makeAlarmOnExcessPolution);
            return result;
        } catch (final CloneNotSupportedException ex) {
            throw new AssertionError();
        }
	}

	public float getSpeedCoef(){
		if(speedMode==SPEED_MODE_KMH){
			return COEF_KMH;
		}
		if(speedMode==SPEED_MODE_MPH){
			return COEF_MPH;
		}
		else{
			Log.e(TAG, "No data for speed coef found!");
			return 0f;
		}
	}

	public int getSpeedMode() {
		return speedMode;
	}

	public void setSpeedMode(int speedMode) {
		this.speedMode = speedMode;
	}

	public boolean isFollowNavigation() {
		return followNavigation;
	}


	public void setFollowNavigation(boolean followNavigation) {
		this.followNavigation = followNavigation;
	}
	
	public boolean isUseGps() {
		return useGps;
	}

	public void setUseGps(boolean useGps) {
		this.useGps = useGps;
	}

	public String getAppFilesFolder() {
		return appFilesFolder;
	}

	public void setAppFilesFolder(String appFolder) {
		this.appFilesFolder = appFolder;
	}

}
